package application;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public  class ActionTetris implements ActionListener{

	private Tetris theTetris;

	public ActionTetris(Tetris pTetris){
		super();
		this.setTetris(pTetris);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() instanceof JButton){

			//PLAY BUTTON ON HOME MENU
			if(e.getActionCommand() == application.Utils.buttonPlay){
				this.getTetris().newGame();
			}
			//CONFIGURATION BUTTON ON HOME MENU
			else if(e.getActionCommand() == application.Utils.buttonConfig){
				this.getTetris().getHomeMenuSound().stop();
				this.getTetris().getTetrisMenuBar().windowConfigurer(1);
			}else if(e.getActionCommand() == Utils.buttonNewPlayer){
				String playerName = JOptionPane.showInputDialog(null,
						"Quel est votre nom ?",
						"Nouveau joueur",
						JOptionPane.QUESTION_MESSAGE);
				if(playerName != null && !playerName.equals("")){
					theTetris.getTheTetrisData().addPlayer(playerName);
					theTetris.reloadComboPlayers();
				}
			}
			
			//BOUTON OK (fermer) DANS CONFIGURATION
			else if(e.getActionCommand() == application.Utils.buttonOk){
				this.getTetris().getHomeMenuSound().stop();
				this.getTetris().getTetrisMenuBar().getDialogConfiguration().dispose();
				this.getTetris().getFrame().repaint();
			}
			
		}
		else if(e.getSource() instanceof JCheckBox){
			//CHECKBOX SOUND ON/OFF ON HOME MENU
			if(e.getActionCommand() == application.Utils.boxSound){
				if(((JCheckBox)e.getSource()).isSelected()){
					application.Utils.soundActivated = true;
					this.getTetris().getHomeMenuSound().resume();
				}
				else{
					application.Utils.soundActivated = false;
					this.getTetris().getHomeMenuSound().pause();
				}
				//this.getTetris().getHomeMenuSound().togglePause();
			}
			this.getTetris().getFrame().requestFocusInWindow();
		}
		else if(e.getSource() instanceof JComboBox){
			JComboBox jcb = (JComboBox) e.getSource();
			
			int index = ((JComboBox) e.getSource()).getSelectedIndex();

			/*if(application.Utils.getThemes().size() >= index){
				application.Utils.setCurrentTheme(index);
			}*/

			theTetris.getTheTetrisData().setPlayer((String)jcb.getItemAt(index));
		}
	}


	public Tetris getTetris() {
		return theTetris;
	}

	public void setTetris(Tetris pTetris) {
		this.theTetris = pTetris;
	}
}